package com.jeecms.wicool.dao.image.impl;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.wicool.dao.image.ImageEntityDao;
import com.jeecms.wicool.entity.image.ImageEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by cuiyanlei on 15/6/7.
 */
@Repository
public class ImageEntityDaoImpl extends HibernateBaseDao<ImageEntity, Integer> implements ImageEntityDao {

    @Override
    public ImageEntity save(ImageEntity bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public ImageEntity deleteById(Integer id) {
        ImageEntity entity = super.get(id);
        if (null != entity) {
            getSession().delete(entity);
        }
        return entity;
    }

    @Override
    public ImageEntity updateByUpdater(Updater<ImageEntity> updater) {
        return null;
    }

    @Override
    protected Class<ImageEntity> getEntityClass() {
        return ImageEntity.class;
    }

    @Override
    public List<ImageEntity> getAll(Integer foreignKey, int type) {
        Finder f = Finder.create(" select bean from ImageEntity bean where 1=1 ");
        f.append(" and bean.relationId=:relation_id ").setParam("relation_id",foreignKey);
        f.append(" and bean.imgType=:imgType").setParam("imgType", type);
        f.append(" order by bean.order desc ");
        return find(f);
    }
}
